#include "adc_driver.h"
#include <board.h>
#include <rtdevice.h>

/* 假设的ADC通道定义，需要根据实际硬件修改 */
static const char *adc_channels[ADC_CHANNELS] = {
    "adc1", /* 通道0 */
    "adc1", /* 通道1 */
    "adc1", /* 通道2 */
    "adc1"  /* 通道3 */
};

static rt_adc_device_t adc_dev;

rt_err_t adc_driver_init(void)
{
    /* 查找ADC设备 */
    adc_dev = (rt_adc_device_t)rt_device_find("adc1");
    if (adc_dev == RT_NULL) {
        LOG_E("ADC device not found\n");
        return RT_ERR_NOT_FOUND;
    }

    /* 使能ADC通道 */
    for (int i = 0; i < ADC_CHANNELS; i++) {
        rt_adc_enable(adc_dev, i);
    }

    return RT_OK;
}

rt_uint16_t adc_read_raw(uint8_t channel)
{
    if (channel >= ADC_CHANNELS) {
        return 0;
    }

    return rt_adc_read(adc_dev, channel);
}

rt_err_t adc_read_all(rt_uint16_t *values, uint8_t count)
{
    if (count > ADC_CHANNELS) {
        return RT_ERR_PARAM;
    }

    for (int i = 0; i < count; i++) {
        values[i] = adc_read_raw(i);
    }

    return RT_OK;
}